package com.jy.adscm.operation.hr.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyMenu;
import com.jy.adscm.bean.system.JyRole;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.hr.bean.HrStationManage;
import com.jy.adscm.operation.hr.mapper.mysql.HrStationManageMapper;
import com.jy.adscm.operation.hr.service.HrStationManageService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyRoleMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.system.service.JyMenuService;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 人力资源-岗位管理 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2020-01-07
 */
@Service
public class HrStationManageServiceImpl extends ServiceImpl<HrStationManageMapper, HrStationManage> implements HrStationManageService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyMenuService jyMenuService;
    @Autowired
    JyRoleMapper jyRoleMapper;
    @Autowired
    HrStationManageMapper hrStationManageMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertHrStationManage(HrStationManage hrStationManage)throws AdscmSystemException {
        if (AdscmBaseUtils.getInstance().isNotEmpty(hrStationManage.getId(),null)){
            JyMenu jyMenu = jyMenuService.selectById(hrStationManage.getId());
            hrStationManage.setName(jyMenu.getName());
        }else {
            throw new AdscmSystemException("菜单ID为空");
        }
        boolean insert = super.insert(hrStationManage);
        return insert;
    }

    @Override
    public List<JyMenu> selectMenu() {
        Wrapper<JyMenu> wrapper=new EntityWrapper<>();
        wrapper.eq("parentId",5195);
        wrapper.eq("srcType",1);
        List<JyMenu> jyMenus = jyMenuService.selectList(wrapper);
        getAllChildren(jyMenus);
        return jyMenus;
    }

    public void   getAllChildren(List<JyMenu> jyMenus){
        for (JyMenu jyMenu : jyMenus) {
            Wrapper<JyMenu> wrapper=new EntityWrapper<>();
            wrapper.eq("parentId",jyMenu.getId());
            wrapper.eq("srcType",1);
            List<JyMenu> children = jyMenuService.selectList(wrapper);
            if (children!=null&&children.size()>0){
                getAllChildren(children);
            }
            jyMenu.setChildren(children);
        }
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addMenu(JyMenu jyMenu) throws AdscmSystemException {
        jyMenu.setSrcType(1);
        jyMenuService.addMenu(jyMenu);
        jyMenu.setPath("/adscm/humanResources/index?orgId="+jyMenu.getId());
        jyMenu.setUrl("/adscm/humanResources/index?orgId="+jyMenu.getId());
        jyMenuService.updateById(jyMenu);
        //在法规查看者里面添加这个菜单
        Wrapper<JyRole> wrapper=new EntityWrapper<>();
        wrapper.eq("role_code","ROLE_hr_viewer");
        List<JyRole> jyRoles = jyRoleMapper.selectList(wrapper);
        if (jyRoles!=null&&jyRoles.size()>0){
            JyRole jyRole = jyRoles.get(0);
            jyRoleMapper.setRoleMenus(jyRole.getId(), Arrays.asList(jyMenu.getId()));
        }
        return true;
    }

    @Override
    public Boolean deleteMenu(Integer id) throws AdscmSystemException{
        jyMenuService.delMenu(id);
        //删除这个角色对应的菜单
        Wrapper<JyRole> wrapper=new EntityWrapper<>();
        wrapper.eq("role_code","ROLE_hr_viewer");
        List<JyRole> jyRoles = jyRoleMapper.selectList(wrapper);
        if (jyRoles!=null&&jyRoles.size()>0){
            JyRole jyRole = jyRoles.get(0);
            jyRoleMapper.deleteRoleMenu(jyRole.getId(),id);
        }
        return true;
    }

    @Override
    public HrStationManage selectStationDescById(Integer id) {

        HrStationManage hrStationManage=    hrStationManageMapper.selectStationDescById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(hrStationManage,null)){
            hrStationManage = selectUser(hrStationManage);
        }else {
            return null;
        }
        return hrStationManage;
    }
    public HrStationManage  selectUser(HrStationManage hrStationManage){
        if (AdscmBaseUtils.getInstance().isNotEmpty(hrStationManage.getCreator(),null)){
            JyUser jyUser = jyUserMapper.selectById(hrStationManage.getCreator());
            hrStationManage.setCreatorName(jyUser.getName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(hrStationManage.getRecruitmentDescCreator(),null)){
            JyUser jyUser = jyUserMapper.selectById(hrStationManage.getRecruitmentDescCreator());
            hrStationManage.setRecruitmentDescCreatorName(jyUser.getName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(hrStationManage.getStationDescCreator(),null)){
            JyUser jyUser = jyUserMapper.selectById(hrStationManage.getStationDescCreator());
            hrStationManage.setStationDescCreatorName(jyUser.getName());
        }
        return hrStationManage;
    }



    @Override
    public HrStationManage selectRecruitmentDescById(Integer id) {
        HrStationManage hrStationManage=    hrStationManageMapper.selectRecruitmentDescById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(hrStationManage,null)){
            hrStationManage = selectUser(hrStationManage);
        }else {
            return null;
        }
        return hrStationManage;
    }
    @Override
    public Boolean updateStationDesc(HrStationManage hrStationManage) {
        HrStationManage old = super.selectById(hrStationManage.getId());
        old.setStationDesc(hrStationManage.getStationDesc());
        old.setStationDescCreator(hrStationManage.getStationDescCreator());
        old.setStationDescCreateTime(new Date());
        boolean b = super.updateById(old);
        return b;
    }

    @Override
    public Boolean updateRecruitmentDesc(HrStationManage hrStationManage) {
        HrStationManage old = super.selectById(hrStationManage.getId());
        old.setRecruitmentDesc(hrStationManage.getRecruitmentDesc());
        old.setRecruitmentDescCreator(hrStationManage.getRecruitmentDescCreator());
        old.setRecruitmentDescCreateTime(new Date());
        boolean b = super.updateById(old);
        return b;
    }
}
